
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using Csla.Validation;
using BA.DAL;
using BA;

namespace BlazeApps.Library.Page
{
    [Serializable()]
    public class PageSuggestion : Csla.BusinessBase<PageSuggestion>
    {
        #region Business Properties and Methods

        //declare members
        private Guid _id = Guid.NewGuid();
        private SmartDate _createdate = new SmartDate(DateTime.Now);
        private string _suggestdesc = string.Empty;
        private string _suggestedbyemail = string.Empty;
        private int _ivItemtype = 0;
        private string _DBConnection = string.Empty;

        [System.ComponentModel.DataObjectField(true, true)]
        public Guid id
        {
            get
            {
                CanReadProperty("id", true);
                return _id;
            }
        }

        public DateTime createdate
        {
            get
            {
                CanReadProperty("createdate", true);
                return _createdate.Date;
            }
        }

        public string createdateString
        {
            get
            {
                CanReadProperty("createdate", true);
                return _createdate.Text;
            }
            set
            {
                CanWriteProperty("createdate", true);
                if (value == null)
                    value = string.Empty;
                if (!_createdate.Equals(value))
                {
                    _createdate.Text = value;
                    PropertyHasChanged("createdate");
                }
            }
        }

        public string suggestdesc
        {
            get
            {
                CanReadProperty("suggestdesc", true);
                return _suggestdesc;
            }
            set
            {
                CanWriteProperty("suggestdesc", true);
                if (value == null)
                    value = string.Empty;
                if (!_suggestdesc.Equals(value))
                {
                    _suggestdesc = value;
                    PropertyHasChanged("suggestdesc");
                }
            }
        }

        public string suggestedbyemail
        {
            get
            {
                CanReadProperty("suggestedbyemail", true);
                return _suggestedbyemail;
            }
            set
            {
                CanWriteProperty("suggestedbyemail", true);
                if (value == null)
                    value = string.Empty;
                if (!_suggestedbyemail.Equals(value))
                {
                    _suggestedbyemail = value;
                    PropertyHasChanged("suggestedbyemail");
                }
            }
        }

        public int IvItemtype
        {
            get
            {
                CanReadProperty("IvItemtype", true);
                return _ivItemtype;
            }
            set
            {
                CanWriteProperty("IvItemtype", true);
                if (!_ivItemtype.Equals(value))
                {
                    _ivItemtype = value;
                    PropertyHasChanged("IvItemtype");
                }
            }
        }

        public string DBConnection
        {
            get
            {
                CanReadProperty("DBConnection", true);
                return _DBConnection;
            }
            set
            {
                CanWriteProperty("DBConnection", true);
                if (value == null)
                    value = string.Empty;
                if (!_DBConnection.Equals(value))
                {
                    _DBConnection = value;
                    PropertyHasChanged("DBConnection");
                }
            }
        }

        protected override object GetIdValue()
        {
            return _id;
        }

        #endregion //Business Properties and Methods

        #region Validation Rules
        private void AddCustomRules()
        {
            //add custom/non-generated rules here...
        }

        private void AddCommonRules()
        {
            //
            // createdate
            //
            ValidationRules.AddRule(CommonRules.StringRequired, "createdateString");
            //
            // suggestdesc
            //
            ValidationRules.AddRule(CommonRules.StringRequired, "suggestdesc");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new CommonRules.MaxLengthRuleArgs("suggestdesc", 8000));
            //
            // suggestedbyemail
            //
            ValidationRules.AddRule(CommonRules.StringRequired, "suggestedbyemail");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new CommonRules.MaxLengthRuleArgs("suggestedbyemail", 8000));
        }

        protected override void AddBusinessRules()
        {
            AddCommonRules();
            AddCustomRules();
        }
        #endregion //Validation Rules

        #region Authorization Rules
        protected override void AddAuthorizationRules()
        {
            //TODO: Define authorization rules in pageSuggestion
            //AuthorizationRules.AllowRead("id", "pageSuggestionReadGroup");
            //AuthorizationRules.AllowRead("createdate", "pageSuggestionReadGroup");
            //AuthorizationRules.AllowRead("suggestdesc", "pageSuggestionReadGroup");
            //AuthorizationRules.AllowRead("suggestedbyemail", "pageSuggestionReadGroup");
            //AuthorizationRules.AllowRead("IvItemtype", "pageSuggestionReadGroup");

            //AuthorizationRules.AllowWrite("createdate", "pageSuggestionWriteGroup");
            //AuthorizationRules.AllowWrite("suggestdesc", "pageSuggestionWriteGroup");
            //AuthorizationRules.AllowWrite("suggestedbyemail", "pageSuggestionWriteGroup");
            //AuthorizationRules.AllowWrite("IvItemtype", "pageSuggestionWriteGroup");
        }


        public static bool CanGetObject()
        {
            //TODO: Define CanGetObject permission in pageSuggestion
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("pageSuggestionViewGroup"))
            //	return true;
            //return false;
        }

        public static bool CanAddObject()
        {
            //TODO: Define CanAddObject permission in pageSuggestion
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("pageSuggestionAddGroup"))
            //	return true;
            //return false;
        }

        public static bool CanEditObject()
        {
            //TODO: Define CanEditObject permission in pageSuggestion
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("pageSuggestionEditGroup"))
            //	return true;
            //return false;
        }

        public static bool CanDeleteObject()
        {
            //TODO: Define CanDeleteObject permission in pageSuggestion
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("pageSuggestionDeleteGroup"))
            //	return true;
            //return false;
        }
        #endregion //Authorization Rules

        #region Factory Methods
        private PageSuggestion()
        { /* require use of factory method */
        }

        public static PageSuggestion NewpageSuggestion()
        {
            if (!CanAddObject())
                throw new System.Security.SecurityException("User not authorized to add a pageSuggestion");
            return DataPortal.Create<PageSuggestion>();
        }

        public static PageSuggestion GetpageSuggestion(Guid id)
        {
            if (!CanGetObject())
                throw new System.Security.SecurityException("User not authorized to view a pageSuggestion");
            return DataPortal.Fetch<PageSuggestion>(new Criteria(id));
        }

        public static void DeletepageSuggestion(Guid id)
        {
            if (!CanDeleteObject())
                throw new System.Security.SecurityException("User not authorized to remove a pageSuggestion");
            DataPortal.Delete(new Criteria(id));
        }

        public override PageSuggestion Save()
        {
            if (IsDeleted && !CanDeleteObject())
                throw new System.Security.SecurityException("User not authorized to remove a pageSuggestion");
            else if (IsNew && !CanAddObject())
                throw new System.Security.SecurityException("User not authorized to add a pageSuggestion");
            else if (!CanEditObject())
                throw new System.Security.SecurityException("User not authorized to update a pageSuggestion");

            return base.Save();
        }

        #endregion //Factory Methods

        #region Data Access

        #region Criteria

        [Serializable()]
        private class Criteria
        {
            public Guid id;

            public Criteria(Guid id)
            {
                this.id = id;
            }
        }

        #endregion //Criteria

        #region Data Access - Create
        [RunLocal]
        protected override void DataPortal_Create()
        {
            ValidationRules.CheckRules();
        }

        #endregion //Data Access - Create

        #region Data Access - Fetch
        //private void DataPortal_Fetch(Criteria criteria)
        //{
        //    using (SqlConnection cn = new SqlConnection(Database.powerKBConnection))
        //    {
        //        cn.Open();

        //        ExecuteFetch(cn, criteria);
        //    }//using
        //}

        //private void ExecuteFetch(SqlConnection cn, Criteria criteria)
        //{
        //    using (SqlCommand cm = cn.CreateCommand())
        //    {
        //        cm.CommandType = CommandType.StoredProcedure;
        //        cm.CommandText = "GetpageSuggestion";

        //        cm.Parameters.AddWithValue("@suggestionid", criteria.id);

        //        using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
        //        {
        //            FetchObject(dr);
        //            ValidationRules.CheckRules();

        //            //load child object(s)
        //            FetchChildren(dr);
        //        }
        //    }//using
        //}

        //private void FetchObject(SafeDataReader dr)
        //{
        //    dr.Read();
        //    _id = dr.GetGuid("suggestionid");
        //    _createdate = dr.GetSmartDate("createdate", _createdate.EmptyIsMin);
        //    _suggestdesc = dr.GetString("suggestdesc");
        //    _suggestedbyemail = dr.GetString("suggestedbyemail");
        //    _ivItemtype = dr.GetInt32("iv_itemtype");
        //}

        //private void FetchChildren(SafeDataReader dr)
        //{
        //}
        #endregion //Data Access - Fetch

        #region Data Access - Insert
        protected override void DataPortal_Insert()
        {
            ExecuteInsert();
        }

        /// <summary>
        /// Executes the insert.
        /// </summary>
        private void ExecuteInsert()
        {
            BlazeApps.Library.ConfigSettings Config = new BlazeApps.Library.ConfigSettings();
            BA.DAL.DBUtility DAL = new BA.DAL.DBUtility();
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            AddInsertParameters(ref DAL);

            DAL.ExecuteNonQuery("usp_insert_page_suggestion", CommandType.StoredProcedure, ConnectionState.Open);
        }

        private void AddInsertParameters(ref BA.DAL.DBUtility DAL)
        {
            DAL.AddParameter("@suggestionid", _id);
            DAL.AddParameter("@suggestdesc", _suggestdesc);
            DAL.AddParameter("@suggestedbyemail", _suggestedbyemail);
        }
        #endregion //Data Access - Insert

        #region Data Access - Update
        //protected override void DataPortal_Update()
        //{
        //    using (SqlConnection cn = new SqlConnection(Database.powerKBConnection))
        //    {
        //        cn.Open();

        //        if (base.IsDirty)
        //        {
        //            ExecuteUpdate(cn);
        //        }

        //        //update child object(s)
        //        UpdateChildren(cn);
        //    }//using

        //}

        //private void ExecuteUpdate(SqlConnection cn)
        //{
        //    using (SqlCommand cm = cn.CreateCommand())
        //    {
        //        cm.CommandType = CommandType.StoredProcedure;
        //        cm.CommandText = "UpdatepageSuggestion";

        //        AddUpdateParameters(cm);

        //        cm.ExecuteNonQuery();

        //    }//using
        //}

        //private void AddUpdateParameters(SqlCommand cm)
        //{
        //    cm.Parameters.AddWithValue("@suggestionid", _id);
        //    cm.Parameters.AddWithValue("@createdate", _createdate.DBValue);
        //    cm.Parameters.AddWithValue("@suggestdesc", _suggestdesc);
        //    cm.Parameters.AddWithValue("@suggestedbyemail", _suggestedbyemail);
        //    cm.Parameters.AddWithValue("@iv_itemtype", _ivItemtype);
        //}

        //private void UpdateChildren(SqlConnection cn)
        //{
        //}
        #endregion //Data Access - Update

        #region Data Access - Delete
        //protected override void DataPortal_DeleteSelf()
        //{
        //    DataPortal_Delete(new Criteria(_id));
        //}

        //private void DataPortal_Delete(Criteria criteria)
        //{
        //    using (SqlConnection cn = new SqlConnection(Database.powerKBConnection))
        //    {
        //        cn.Open();

        //        ExecuteDelete(cn, criteria);

        //    }//using

        //}

        //private void ExecuteDelete(SqlConnection cn, Criteria criteria)
        //{
        //    using (SqlCommand cm = cn.CreateCommand())
        //    {
        //        cm.CommandType = CommandType.StoredProcedure;
        //        cm.CommandText = "DeletepageSuggestion";

        //        cm.Parameters.AddWithValue("@suggestionid", criteria.id);

        //        cm.ExecuteNonQuery();
        //    }//using
        //}
        #endregion //Data Access - Delete
        #endregion //Data Access
    }
}
